package cn.com.self.apis.portal.service.impl;


import cn.com.self.apis.portal.entity.SysRoleEntity;
import cn.com.self.apis.portal.entity.SysUserEntity;
import cn.com.self.apis.portal.entity.SysUserRoleEntity;
import cn.com.self.apis.portal.mapper.SysRoleMapper;
import cn.com.self.apis.portal.mapper.SysUserMapper;
import cn.com.self.apis.portal.mapper.SysUserRoleMapper;
import cn.com.self.apis.portal.service.intf.ISysUserRoleService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.selfsoft.smartcity.core.service.BaseServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description
 */
@Service
public class SysUserRoleService extends BaseServiceImpl<SysUserRoleEntity, String> implements ISysUserRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public ICustomPage<SysUserEntity> selectPage(Map<String, Object> queryParams, Page pageable) {
        QueryWrapper<SysUserRoleEntity> queryWrapper =  new QueryWrapper<>();
        QueryWrapper<SysUserEntity> userQuserWrapper =  new QueryWrapper<>();
        queryParams.keySet().forEach(key -> {
            if(("id").equals(key)){
                queryWrapper.eq("id", queryParams.get("id"));
            }else if("role_id".equals(key)) {
                queryWrapper.eq("role_id",queryParams.get("role_id"));
            }else if("userId".equals(key)) {
                queryWrapper.eq("user_id",queryParams.get("userId"));
            }else if("username".equals(key)) {
                userQuserWrapper.eq("1","1");
                userQuserWrapper.and(wrapper -> wrapper.like("username", queryParams.get("username")).or().like("name", queryParams.get("username")));

                //serQuserWrapper.like("username",queryParams.get("username")).or().like("name", queryParams.get("username"));
            }
        });

        //获取角色已授权关联关系数据
        List<SysUserRoleEntity> list = sysUserRoleMapper.selectList(queryWrapper);
        List<String> userIds = getUserIds(list);
        if (userIds!=null&&userIds.size()>0){
            userQuserWrapper.in("id",userIds);
        }else {
            userQuserWrapper.in("id","");

        }

        //获取符合条件的已授权用户数据
        ICustomPage<SysUserEntity> pageList =  (ICustomPage<SysUserEntity>)sysUserMapper.selectPage(pageable,userQuserWrapper);

        return pageList;
    }

    @Override
    public ICustomPage<SysUserEntity> ntgranuserpage(Map<String, Object> queryParams, Page pageable) {
        QueryWrapper<SysUserRoleEntity> queryWrapper =  new QueryWrapper<>();
        QueryWrapper<SysUserEntity> userQuserWrapper =  new QueryWrapper<>();
        QueryWrapper<SysRoleEntity> roleWrapper =  new QueryWrapper<>();

        queryParams.keySet().forEach(key -> {
            if(("id").equals(key)){
                queryWrapper.eq("id", queryParams.get("id"));
            }else if("role_id".equals(key)) {
                queryWrapper.eq("role_id",queryParams.get("role_id"));
            }else if("userId".equals(key)) {
                queryWrapper.eq("user_id",queryParams.get("userId"));
            }else if("username".equals(key)) {
                userQuserWrapper.like("username",queryParams.get("username"));
            }
        });
        queryParams.keySet().forEach(key -> {
             if("role_id".equals(key)) {
                roleWrapper.eq("id",queryParams.get("role_id"));
            }
        });

        //获取角色已授权关联关系数据
        List<SysUserRoleEntity> list = sysUserRoleMapper.selectList(queryWrapper);
        //获取角色
        SysRoleEntity rolelist = sysRoleMapper.selectOne(roleWrapper);
        //判断角色是否是系统管理员
        String organizationType = "";
        if("309".equals(rolelist.getRoleTyle())){
            organizationType = "1";
            userQuserWrapper.eq("is_admin",organizationType);
        }
        //判断角色是否是汽车管理
        if("310".equals(rolelist.getRoleTyle())){
            organizationType = "202";
            userQuserWrapper.eq("organization_type",organizationType);
        }
        //般的角色是否是审核管理
        if("311".equals(rolelist.getRoleTyle())){
            organizationType = "201";
            userQuserWrapper.eq("organization_type",organizationType);
            userQuserWrapper.ne("is_admin",1);
        }
        List<String> userIds = getUserIds(list);
        if (userIds!=null&&userIds.size()>0){
            userQuserWrapper.notIn("id",userIds);
        }else {
            userQuserWrapper.notIn("id","");

        }

        //获取符合条件的未授权用户数据
        ICustomPage<SysUserEntity> pageList =  (ICustomPage<SysUserEntity>)sysUserMapper.selectPage(pageable,userQuserWrapper);

        return pageList;

    }

    @Override
    public void grantUser(List<SysUserRoleEntity> list) {

        for (SysUserRoleEntity entity:list){
            QueryWrapper<SysUserRoleEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id",entity.getRoleId());
            queryWrapper.eq("user_id",entity.getUserId());
            //判断同角色、同用户是否已经被授权
            SysUserRoleEntity info = sysUserRoleMapper.selectOne(queryWrapper);
            if (info==null){
                entity.setCreateTime(new Date());
                entity.setUpdateTime(new Date());
                sysUserRoleMapper.insert(entity);

            }
        }

    }

    @Override
    public void deletes(List<SysUserRoleEntity> list) {
        for (SysUserRoleEntity info :list){
            QueryWrapper<SysUserRoleEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id",info.getRoleId());
            queryWrapper.eq("user_id",info.getUserId());
            sysUserRoleMapper.delete(queryWrapper);
        }

    }

    /**遍历出用户id
     * @Description //TODO
     * @return
     **/
    private List<String> getUserIds(List<SysUserRoleEntity> list ){
        List<String> userList = new ArrayList<>();
        for (SysUserRoleEntity info:list){
            userList.add(info.getUserId());
        }
        return  userList;
    }

}
